package cn.tedu.ivos.vehicle.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleSaveDTO;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos.vehicle.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.rmi.ServerException;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author liyuehui
 * @time 2024/7/10 9:14
 */
@Slf4j
@Service
public class VehicleServiceImpl implements VehicleService {
    @Autowired
    private VehicleMapper vehicleMapper;

    /**
     * 查询车辆
     * @param vehicleQuery
     * @return List<VehicleVO>
     */
    @Override
    public List<VehicleVO> selectVehicle(VehicleQuery vehicleQuery) {
        return vehicleMapper.selectVehicle(vehicleQuery);
    }

    /**
     * 保存车辆(新增/修改)
     * @param vehicleSaveDTO
     * @return int
     */
    @Override
    public int save(VehicleSaveDTO vehicleSaveDTO) {
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleSaveDTO, vehicle);
        log.debug("保存车辆，参数：{}", vehicle);
        // 新增车辆
        if (vehicle.getId() == null) {
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setLicense(vehicleSaveDTO.getLicense());
            List<VehicleVO> vehicleVOS = vehicleMapper.selectVehicle(vehicleQuery);
            // 查询到的车牌号存在记录
            if (vehicleVOS.size() > 0){
                throw new ServiceException(StatusCode.LICENSE_ALREADY_EXISTS);
            }
            vehicle.setCreateTime(new Date());
            //默认为空闲状态
            vehicle.setStatus("1");
            vehicle.setGeofenceBindStatus("0");
            int result = vehicleMapper.insertVehicle(vehicle);
            // 数据库返回的影响行数
            if (result != 1){
                throw new ServiceException(StatusCode.OPERATION_FAILED);
            }
            return result;
        } else {
            // 更新车辆
            vehicle.setUpdateTime(new Date());
            int result = vehicleMapper.updateVehicle(vehicle);
            if (result != 1){
                throw new ServiceException(StatusCode.OPERATION_FAILED);
            }
            return result;
        }
    }

    /**
     * 删除车辆
     * @param id
     * @return int
     */
    @Override
    public int deleteVehicle(Long id) {
        int result = vehicleMapper.deleteVehicle(id);
        if (result != 1){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
        return result;
    }

    /**
     * 解绑车辆
     * @param id
     * @return int
     */
    @Override
    public int unBind(Long id) {
        Vehicle vehicle = new Vehicle();
        vehicle.setId(id);
        vehicle.setGeofenceBindStatus("0");
        vehicle.setGeofenceId(null);
        vehicle.setUpdateTime(new Date());
        /*
         * 之前的更新sql要求围栏编号不为null才会更新围栏编号
         * 但此处由于解绑车辆业务需要将围栏编号设置为null,所以需要重写一个mapper层的方法
         */
        int result = vehicleMapper.updateNullValue(vehicle);
        if (result != 1){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
        return result;
    }

    /**
     * 绑定车辆
     * @param vehicleId
     * @param geoId
     * @return int
     */
    @Override
    public int bind(Long vehicleId, Long geoId) {
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setGeofenceId(geoId);
        vehicle.setGeofenceBindStatus("1");
        vehicle.setUpdateTime(new Date());
        int result = vehicleMapper.updateVehicle(vehicle);
        if (result != 1){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
        return result;
    }

    /**
     * 查询车辆数量
     * @param vehicleQuery
     * @return int
     */
    @Override
    public int selectCount(VehicleQuery vehicleQuery) {
        int result = vehicleMapper.selectVehicleCount(vehicleQuery);
        if (result != 0){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
        return result;
    }
}
